// vp_cai/pages/index/citys.js
var app = getApp();
var vp = require('../../resource/js/vp.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isClientAdd: false,
    ages: [1, 2, 3, 4, 5, 6, 7, 8, 9],
    index: 0,
    client: {}, // 新增模型
    bl:'',
    bl1:'',
    bl2:'',
    item:{}
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this);
    var self = this;
    let ages = [];
    for (let index = 29; index < 100; index++) {
      ages.push(index + 1);
    }

    self.setData({
      act: options.act ? options.act : '',
      ages
    });

    if (options.act == 'select') {
      wx.setNavigationBarTitle({
        title: '请选择服务对象',
      })
    } else {
      wx.setNavigationBarTitle({
        title: '服务对象管理',
      })
    }

    app.util.getUserInfo(function (response) {
      self.setData({
        userInfo: response
      });
      app.mdInit(function (cfg, mine, area) {
        self.setData({
          cfg: cfg,
          mine: mine,
          area: area
        });

        self.main_load(false);
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onBlImgChange: function () {
    var self = this;
    var url = app.util.url('Common/upload');
    wx.chooseImage({
      count: 1,
      success: function (res) {
        res.tempFilePaths[0],
        wx.uploadFile({
          url: url,
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'path': res.tempFilePaths[0]
          },
          success: function (ret) {
            ret.data = JSON.parse(ret.data);
            console.log(ret.data);

            if (ret.data.code != 1) {
              wx.showModal({
                title: '图片上传失败',
                content: ret.data.message,
                showCancel: false,
                success(res) {
                  self.setData({
                    bl: '',
                  });
                }
              });
            } else {
              self.setData({
                bl: ret.data.data.url
              });
            }
          }
        });
      }
    });
  },
  onBl1ImgChange: function () {
    var self = this;
    var url = app.util.url('Common/upload');
    wx.chooseImage({
      count: 1,
      success: function (res) {
        res.tempFilePaths[0],
        wx.uploadFile({
          url: url,
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'path': res.tempFilePaths[0]
          },
          success: function (ret) {
            ret.data = JSON.parse(ret.data);
            console.log(ret.data);

            if (ret.data.code != 1) {
              wx.showModal({
                title: '图片上传失败',
                content: ret.data.message,
                showCancel: false,
                success(res) {
                  self.setData({
                    bl1: ''
                  });
                }
              });
            } else {
              self.setData({
                bl1: ret.data.data.url
              });
            }
          }
        });
      }
    });
  },
  onBl2ImgChange: function () {
    var self = this;
    var url = app.util.url('Common/upload');
    wx.chooseImage({
      count: 1,
      success: function (res) {
        res.tempFilePaths[0],

        wx.uploadFile({
          url: url,
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'path': res.tempFilePaths[0]
          },
          success: function (ret) {
            ret.data = JSON.parse(ret.data);
            console.log(ret.data);

            if (ret.data.code != 1) {
              wx.showModal({
                title: '图片上传失败',
                content: ret.data.message,
                showCancel: false,
                success(res) {
                  self.setData({
                   bl2: ''
                  });
                }
              });
            } else {
              self.setData({
                bl2: ret.data.data.url
              });
            }
          }
        });
      }
    });
  },
  // 页面数据加载
  main_load(showLoading) {
    const self = this;
    //console.log("self.data.like "+self.data.like);
    app.util.request({
      url: 'User/clients',
      showLoading: showLoading ? true : false,
      data: {
        m: app.mdName,
        aid: self.data.area.id
      },
      success(res) {
        var data = res.data.data;
        self.setData({
          main_loaded: true,
          now: data.now,
          clients: data.clients,
          isClientAdd: ((data.clients && data.clients.length > 0) ? false : true)
        });
      }
    })
  },

  onClientSelected: function (e) {
    if (this.data.act == '') {
      return;
    }
    var index = e.currentTarget.dataset.index
    var client = this.data.clients[index];

    let pages = getCurrentPages();
    if (pages.length >= 2) {
      var prevPage = pages[pages.length - 2]; //上一个页面
      //给pageA页面赋值
      prevPage.setData({
        client: client
      });

      wx.navigateBack();
    }
  },

  removeClient: function (e) {
    var self = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定移除该服务对象信息？',
      success(res) {
        if (res.confirm) {
          app.util.request({
            url: 'User/client',
            data: {
              m: app.mdName,
              aid: self.data.area.id,
              submit: 'remove',
              id: id
            },
            success(res) {
              wx.showToast({
                title: res.data.msg,
                icon: 'success',
                duration: 2000,
                success: function () {
                  setTimeout(function () {
                    self.main_load(true);
                  }, 1800);
                }
              });
            }
          });
        }
      }
    });
  },


  switchClientAdd: function (e) {
    this.setData({
      isClientAdd: !this.data.isClientAdd,
      client:{},
      bl:'',
      bl1:'',
      bl2:''
    });
  },
  qx: function (e) {
    this.setData({
      isClientAdd: !this.data.isClientAdd
    });
  },
  switchClientUpdate: function (e) {
    let item=this.data.clients[e.currentTarget.dataset.index]
    console.log(item);
    let bl=this.data.clients[e.currentTarget.dataset.index].bl
    let bl1=this.data.clients[e.currentTarget.dataset.index].bl1
    let bl2=this.data.clients[e.currentTarget.dataset.index].bl2
    this.setData({
      isClientAdd: !this.data.isClientAdd,
      client:item,
      item:item,
      bl:bl,
      bl1:bl1,
      bl2:bl2
    });
  },
  clear: function (e) {
    
    let name=e.currentTarget.dataset.name
    console.log(name);
    if(name=='bl'){
      this.setData({
        bl:'',
      });
    }
    if(name=='bl1'){
      this.setData({
        bl1:'',
      });
    }
    if(name=='bl2'){
      this.setData({
        bl2:'',
      });
    }
  },
  onSexChange: function (e) {
    this.setData({
      'client.sex': e.currentTarget.dataset.sex
    });
  },


  clientSave: function (e) {
    var self = this;

    if (self.data._is_saving) {
      return;
    }

    var client = this.data.client;

    if (!e.detail.value.name) {
      return wx.showToast({
        title: '请填写就诊人姓名',
        icon: 'none',
        duration: 2000
      });
    }
    client.name = e.detail.value.name;

    if (!(client.sex > 0)) {
      return wx.showToast({
        title: '请选择服务对象性别',
        icon: 'none',
        duration: 2000
      });
    }

    if (!self.data.ages[self.data.index]) {
      return wx.showToast({
        title: '请填写服务对象年龄',
        icon: 'none',
        duration: 2000
      });
    }
    client.age = self.data.ages[self.data.index];

    if (!e.detail.value.mobile) {
      return wx.showToast({
        title: '请填写服务对象手机号',
        icon: 'none',
        duration: 2000
      });
    }
    client.mobile = e.detail.value.mobile;
    client.bs = e.detail.value.bs;
    client.bl = self.data.bl;
    client.bl1 =self.data.bl1;
    client.bl2 =self.data.bl2;
    // 序列化提交数据对象
    var form = encodeURI(JSON.stringify(client));

    self.setData({
      _is_saving: true
    });

    app.util.request({
      url: 'User/client',
      data: {
        m: app.mdName,
        aid: self.data.area.id,
        submit: 'save',
        form: form
      },
      complete(res) {
        self.setData({
          _is_saving: false
        });
      },
      success(res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000,
          success: function () {
            setTimeout(function () {
              self.main_load();
            }, 2000);
          }
        });
      }
    })
  },



})